package org.ws.edu.task2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.ws.edu.task2.commands.Copy;
import org.ws.edu.task2.commands.MakeFile;
import org.ws.edu.task2.commands.MakeFolder;
import org.ws.edu.task2.commands.Move;
import org.ws.edu.task2.commands.Remove;
import org.ws.edu.task2.exceptions.ExtensionLengthException;
import org.ws.edu.task2.exceptions.IncorrectPathException;
import org.ws.edu.task2.exceptions.IncorrectSymbolsException;
import org.ws.edu.task2.exceptions.NameLengthException;

public class MenuDispatcher {

    private String commandName = null;
    private String firstPath = null;
    private String secondPath = null;
    private VirtualFileSystem vfs = null;

    public MenuDispatcher() {
        vfs = VirtualFileSystem.getInstance();
    }

    public void run() {
        while (true) {
            System.out.println("Input the path to batch file:");
            BufferedReader inPath = new BufferedReader(new InputStreamReader(System.in));
            String filePath;
            try {
                filePath = inPath.readLine();
                if (filePath.equals("exit")) {
                    break;
                }
                BufferedReader inCommands = new BufferedReader(new FileReader(filePath));
                String line = null;
                while ((line = inCommands.readLine()) != null) {
                    line = line.toUpperCase();
                    parseLine(line);
                    if (commandName.equals("MD")) {
                        new MakeFolder(firstPath).execute(vfs);
                    } else if (commandName.equals("MF")) {
                        new MakeFile(firstPath).execute(vfs);
                    } else if (commandName.equals("RM")) {
                        new Remove(firstPath).execute(vfs);
                    } else if (commandName.equals("COPY")) {
                        new Copy(firstPath, secondPath).execute(vfs);
                    } else if (commandName.equals("MOVE")) {
                        new Move(firstPath, secondPath).execute(vfs);
                    }
                }
                printResult(vfs.getRootElement());

            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("IO Exception");
            } catch (IncorrectPathException e) {
                e.printStackTrace();
                System.err.println("IncorrectPath");
            } catch (ExtensionLengthException e) {
                e.printStackTrace();
                System.err.println("Excess of symbol limit in extension");
            } catch (NameLengthException e) {
                e.printStackTrace();
                System.err.println("Excess of symbol limit in name");
            } catch (IncorrectSymbolsException e) {
                e.printStackTrace();
                System.err.println("Incorrect symbols in path");
            }
        }
    }

    public void parseLine(String line) {

        String[] lines = line.split(" ");
        commandName = lines[0];
        firstPath = lines[1];
        if (lines.length == 3)
            secondPath = lines[2];

    }

    public void printResult(Element rootElement) {
        Element element = rootElement;
        System.out.println(element);
        Iterator iter = element.elements.iterator();
        while (iter.hasNext()) {
            intermediatePrint("|_", (Element) iter.next());

        }
    }

    public void intermediatePrint(String lines, Element element) {
        String line = lines;
        line = "| " + line;
        System.out.println(line + element);
        if (!element.elements.isEmpty()) {
            Iterator iter = element.elements.iterator();
            while (iter.hasNext()) {
                intermediatePrint(line, (Element) iter.next());
            }
        }

    }
}
